package com.android.namecard.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.android.namecard.R;
import com.android.namecard.model.NamecardDBHelper;

public class SelfDefineActivity extends Activity implements OnGestureListener{
	private ViewFlipper flipper;

	private GestureDetector detector;
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.self_define);
		detector = new GestureDetector(this);
		flipper = (ViewFlipper) this.findViewById(R.id.ViewFlipper01);
        
        //获取界面上的两个按钮
        Button frontBut = (Button)findViewById(R.id.front_img_scan);
        Button backBut = (Button)findViewById(R.id.back_img_scan);
        Button submitBut = (Button)findViewById(R.id.submit);
        //浏览按钮（名片正面）
        OnClickListener listener1 = new OnClickListener(){
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(SelfDefineActivity.this, FileScanActivity.class);
				Bundle bundle = new Bundle();
				bundle.putInt("button", 0);
				intent.putExtras(bundle);
				startActivityForResult(intent,0);
			}
        };
        //浏览按钮（名片背面）
        OnClickListener listener2 = new OnClickListener(){
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(SelfDefineActivity.this, FileScanActivity.class);
				Bundle bundle = new Bundle();
				bundle.putInt("button", 1);
				intent.putExtras(bundle);
				startActivityForResult(intent,0);
			}
        };
        //提交按钮
        OnClickListener listener3 = new OnClickListener(){
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				submit(v);
				SelfDefineActivity.this.finish();
			}
        };
        
        frontBut.setOnClickListener(listener1);
        backBut.setOnClickListener(listener2);
        submitBut.setOnClickListener(listener3);
    }

    //提交按钮
    private void submit(View v){
    	TextView frontImgText = (TextView)findViewById(R.id.front_img_path);
    	TextView backImgText = (TextView)findViewById(R.id.back_img_path);
    	TextView nameText = (TextView)findViewById(R.id.name);
    	TextView careerText = (TextView)findViewById(R.id.career);
    	TextView companyText = (TextView)findViewById(R.id.company);
    	TextView addressText = (TextView)findViewById(R.id.address);
    	TextView emailText = (TextView)findViewById(R.id.email);
    	TextView phoneText = (TextView)findViewById(R.id.phone);
    	TextView qqText = (TextView)findViewById(R.id.qq);
    	TextView msnText = (TextView)findViewById(R.id.msn);
    	TextView websiteText = (TextView)findViewById(R.id.weibsite);
    	TextView businessText = (TextView)findViewById(R.id.business);
    	
    	String frontImgPath = frontImgText.getText().toString();
    	String backImgPath = backImgText.getText().toString();
    	String name = nameText.getText().toString();
    	String career = careerText.getText().toString();
    	String company = companyText.getText().toString();
    	String address = addressText.getText().toString();
    	String email = emailText.getText().toString();
    	String phone = phoneText.getText().toString();
    	String qq = qqText.getText().toString();
    	String msn = msnText.getText().toString();
    	String website = websiteText.getText().toString();
    	String business = businessText.getText().toString();
    	String type = "private";
    	
    	Bitmap frontImg = BitmapFactory.decodeFile(frontImgPath);
    	Bitmap backImg = BitmapFactory.decodeFile(backImgPath);
    	
    	NamecardDBHelper namecardDB = new NamecardDBHelper(v.getContext());
    	namecardDB.insertNamecard(name, career, company, address, email, phone, qq, msn, website, business, type, frontImg, backImg);
    	
    }
    
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		switch(resultCode){
		case 0:
		{
			Bundle bunde = data.getExtras();
			String path = bunde.getString("path");
			EditText frontText = (EditText)findViewById(R.id.front_img_path);
			frontText.setText(path);
			break;
		}
		case 1:
		{
			Bundle bunde = data.getExtras();
			String path = bunde.getString("path");
			EditText backText = (EditText)findViewById(R.id.back_img_path);
			backText.setText(path);
			break;
		}
		default:
			break;
		}
	}

	@Override
	public boolean onDown(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		// TODO Auto-generated method stub
		if (e1.getX() - e2.getX() > 100) {
			this.flipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in));
			this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_out));
			this.flipper.showNext();
			return true;
		} else if (e1.getX() - e2.getX() < -100) {
			this.flipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_right_in));
			this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_right_out));
			this.flipper.showPrevious();
			return true;
		}
		return false;
	}

	@Override
	public void onLongPress(MotionEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onShowPress(MotionEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}
    
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return this.detector.onTouchEvent(event);
	}
}
